﻿using System.Collections.ObjectModel;
using System.Text;
using VideoTools.vo;
using TaskStatus = VideoTools.vo.TaskStatus;

namespace VideoTools.Tasks
{
    public class SplitVideoTaskItem : NotifyPropertyBase
    {
        private bool _killed = false;
        public bool Killed
        {
            get => _killed;
            set { SetProperty(ref _killed, value); }
        }
        private string _Path;
        public string Path
        {
            get => _Path;
            set { SetProperty(ref _Path, value); }
        }

        private string _OutPath;
        public string OutPath
        {
            get => _OutPath;
            set { SetProperty(ref _OutPath, value); }
        }
        private string _Command;
        public string Command
        {
            get => _Command;
            set { SetProperty(ref _Command, value); }
        }
        private TaskStatus _status;
        public TaskStatus Status
        {
            get => _status;
            set
            {
                if (Killed)
                {
                    SetProperty(ref _status, TaskStatus.Failed);
                }
                else
                {
                    SetProperty(ref _status, value);
                }
            }
        }
        private TimeSpan _Duration;
        public TimeSpan Duration
        {
            get => _Duration;
            set => SetProperty(ref _Duration, value);
        }
        private string _FrameSize;
        public string FrameSize
        {
            get => _FrameSize;
            set => SetProperty(ref _FrameSize, value);
        }
        private string _AudioChannel;
        public string AudioChannel
        {
            get => _AudioChannel;
            set => SetProperty(ref _AudioChannel, value);
        }
        private double _progress;
        public double Progress
        {
            get => _progress;
            set { SetProperty(ref _progress, value); }          
        }
        public DateTime taskStartTime;
        private string _StartTime;
        public string StartTime
        {
            get => _StartTime;
            set => SetProperty(ref _StartTime, value);
        }
        private string _EndTime;
        public string EndTime
        {
            get => _EndTime;
            set => SetProperty(ref _EndTime, value);
        }
        private TimeSpan _SpendTime;
        public TimeSpan SpendTime
        {
            get => _SpendTime;
            set => SetProperty(ref _SpendTime, value);
        }
        public ObservableCollection<LogParams> _Logs = new ObservableCollection<LogParams>();
        public ObservableCollection<LogParams> Logs
        {
            get => _Logs;
            set => SetProperty(ref _Logs, value);
        }

        private ListBoxLineBackground _BgColour = ListBoxLineBackground.Even;

        public ListBoxLineBackground BgColour
        {
            get => _BgColour;
            set { SetProperty(ref _BgColour, value); }
        }

        public string CopyLog()
        {
            StringBuilder sb = new();
            foreach (var log in Logs.ToArray())
            {
                if (log.Message.Contains(AppConstants.DoubleClickMessage))
                {
                    continue;
                }
                sb.Append(log.Time.ToString("yyyy-MM-dd HH:mm:ss"));
                sb.Append(' ');
                sb.Append(log.Message);
                sb.Append(Environment.NewLine);
            }
            return sb.ToString();
        }

        public Action<CancellationToken, SplitVideoTaskItem> PreTaskAction { get; set; }

        public Action TaskAction { get; set; }
        public Action TaskActionBefore { get; set; }
        public Action TaskActionAfter { get; set; }
        public CancellationTokenSource CancellationTokenSource { get; set; } =
            new CancellationTokenSource();

        public void Cancel()
        {
            CancellationTokenSource?.Cancel();
            Status = TaskStatus.Failed;
        }

        public void Info(string format, params object[] args)
        {
            App.Current.Dispatcher.BeginInvoke(
                new System.Action(() =>
                {
                    //要执行的代码
                    if (format != null)
                    {
                        Logs.Add(new LogParams(LogType.Info, string.Format(format, args)));
                    }
                })
            );
        }

        public void Warn(string format, params object[] args)
        {
            App.Current.Dispatcher.BeginInvoke(
                new System.Action(() =>
                {
                    //要执行的代码
                    if (format != null)
                    {
                        Logs.Add(new LogParams(LogType.Warning, string.Format(format, args)));
                    }
                })
            );
        }

        public void Error(Exception exception)
        {
            App.Current.Dispatcher.BeginInvoke(
                new System.Action(() =>
                {
                    //要执行的代码
                    if (exception != null)
                    {
                        Logs.Add(new LogParams(LogType.Error, exception.Message));
                    }
                })
            );
        }
    }
}
